package com.chris.oauth2.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chris.oauth2.C;
import com.chris.oauth2.Client;

@Service
public class AuthService {
	@Autowired
	private ClientService clientService;

	public void addAuthCode(String authCode, String clientId) {
		clientService.updateCode(clientId, authCode);
	}

	public void addAccessToken(String accessToken, String clientId) {
		clientService.updateAccessToken(clientId, accessToken);
	}

	public void addRefreshToken(String refreshToken, String clientId) {
		clientService.updateRefreshToken(clientId, refreshToken);
	}

	public void clear(String clientId) {
		clientService.clearClient(clientId);
	}

	public String checkAuthCode(String authCode) {
		return clientService.checkCode(authCode);
	}

	public boolean checkAccessToken(String accessToken) {
		return clientService.checkAccessToken(accessToken);
	}

	public boolean checkRefreshToken(String refreshToken) {
		return clientService.checkRefreshToken(refreshToken);
	}

	public String getClientIdByAccessToken(String accessToken) {
		Client client = clientService.findByAccessToken(accessToken);
		return client != null ? client.getCid() : null;
	}

	public String getClientIdByRefreshToken(String refreshToken) {
		Client client = clientService.findByRefreshToken(refreshToken);
		return client != null ? client.getCid() : null;
	}

	public long getExpireIn() {
		return C.ACCESS_TOKEN_EXPIRE_IN;
	}

	public boolean checkClientId(String clientId) {
		return clientService.findByClientId(clientId) != null;
	}

	public boolean checkClientSecret(String clientSecret) {
		return clientService.findBySecret(clientSecret) != null;
	}

}
